package com.cheer.ba.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.listener.OnItemClickListener;
import com.cheer.ba.model.OilCardInfoModel;
import com.cheer.ba.utils.StringUtils;
import com.cheer.lib_recycle_swipe.SwipeMenuAdapter;

import java.util.List;

/**
 * Created by Hq on 2017/8/2.
 */
public class DriverListAdp extends SwipeMenuAdapter<DriverListAdp.DefaultViewHolder> {

    //可滑动
    public static final int VIEW_TYPE_MENU = 1;
    //不可滑动
    public static final int VIEW_TYPE_NONE = 2;

    private List<OilCardInfoModel> mOilCards;

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public DriverListAdp(List<OilCardInfoModel> mOilCards) {
        this.mOilCards = mOilCards;
    }

    @Override
    public int getItemViewType(int position) {
        return VIEW_TYPE_MENU;
    }

    @Override
    public int getItemCount() {
        return mOilCards == null ? 0 : mOilCards.size();
    }

    public OilCardInfoModel getItem(int position) {
        if (null != mOilCards && !mOilCards.isEmpty()) {
            return mOilCards.get(position);
        }
        return new OilCardInfoModel();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.adp_driver_item, parent, false);
    }

    @Override
    public DefaultViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new DefaultViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(DefaultViewHolder holder, int position) {
        OilCardInfoModel model = getItem(position);
        if (null != model) {
            if (StringUtils.isEmpty(model.getPlateNumber())) {
                holder.mTxtCarNum.setText("车牌号:未知");
            } else {
                holder.mTxtCarNum.setText("车牌号:" + model.getPlateNumber());
            }
            if (StringUtils.isEmpty(model.getCardNum())) {
                holder.mTxtCardNum.setText("卡号: 未关联");
                holder.mTxtOilCard.setText("关联油卡");
            } else {
                holder.mTxtCardNum.setText("卡号: " + model.getCardNum());
                holder.mTxtOilCard.setText("查看流水");
            }
            holder.mTxtDriverName.setText(model.getName());
            holder.mTxtTel.setText(model.getTel());
            holder.setOnItemClickListener(mOnItemClickListener);
        }
    }

    static class DefaultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        //车牌号
        TextView mTxtCarNum;
        //关联油卡
        TextView mTxtOilCard;
        //卡号
        TextView mTxtCardNum;
        //司机名称
        TextView mTxtDriverName;
        //司机电话
        TextView mTxtTel;

        OnItemClickListener mOnItemClickListener;

        public DefaultViewHolder(View itemView) {
            super(itemView);
            mTxtCarNum = (TextView) itemView.findViewById(R.id.id_driver_car_num_txt);

            mTxtOilCard = (TextView) itemView.findViewById(R.id.id_driver_change_txt);
            mTxtOilCard.setOnClickListener(this);
            mTxtCardNum = (TextView) itemView.findViewById(R.id.id_driver_oil_num_txt);
            mTxtDriverName = (TextView) itemView.findViewById(R.id.id_driver_name_txt);
            mTxtTel = (TextView) itemView.findViewById(R.id.id_driver_tel_txt);
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.mOnItemClickListener = onItemClickListener;
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }
}
